<template>
  <div>
    <!-- 搜索框 -->
    <TopFilter
      :list="topFilterList"
      :cols="4"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle">
    </TopFilter>

    <!-- 答案信息 -->
    <div class="box">
      答案信息
      <FilterTable
        ref="table"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        :height="'auto'"
        :isSelectColumn="false"
        :isToperInfo="false"
        :onPageChange="clearAnswerTable"
        columnsRef="ysjanswerTable"
        :onColumnsChange="columns => (this.columns = columns)"
      >
        <template slot="controls">
          <el-button size="small" type="primary" icon="el-icon-plus" @click="add()">新增</el-button>
        </template>
      </FilterTable>
    </div>

    <!-- 选项信息 -->
    <div class="box">
      选项信息
      <FilterTable
        ref="answerTable"
        :columns="answerTableColums"
        :fetchapi="getAnswerApi"
        :params="fetchAnswerParams"
        :datakey="'records'"
        :height="'height:auto'"
        :isSelectColumn="false"
        :isToperInfo="false"
        :isColumnFilter="false"
        columnsRef="ysjanswerAnswerTable"
        :onSyncTableData="tableChangeFun"
        :onColumnsChange="columns => (this.answerTableColums = columns)"
      >
      </FilterTable>
    </div>

    <!-- 新增/编辑答案 -->
    <Drawer
      :visible.sync="visible"
      :title="(type == 'add' ? '新增' : '编辑') + '答案'"
      :width="920"
      destroyOnClose
      :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }"
    >
      <AnswerPanel :type="type" :recordVersion="recordVersion" :answerGroupId="editAnswerGroupId" @changeHandle="changeHandle" @close="closeHandler" />
    </Drawer>
  </div>
</template>
<script>
import { mapState } from 'vuex';
import { dictionary } from '@/utils/dictMixin';
import { getAnswerInfo, delAnswerGroupAndAnswerById, queryAnswersPagByIds } from '@/api/customerCare/visitRules/questionnaireMake';
import AnswerPanel from './answerPanel';

export default {
  name: 'Answer',
  mixins: [dictionary],
  components: {
    AnswerPanel
  },
  data() {
    this.BaseTable = null;
    this.AnswerTable = null;
    return {
      topFilterList: this.createTopFilterList(),
      fetchApi: getAnswerInfo, //获取答案表格接口
      fetchParams: {}, //获取答案表格参数
      columns: this.createTableColumns(), //答案信息表格列
      getAnswerApi: queryAnswersPagByIds, //获取选项表格接口
      fetchAnswerParams: {}, //获取选项表格参数
      answerTableColums: this.createAnswerTableColums(), //选项信息表格列
      visible: false, //控制新增编辑弹框显示
      type: '', //新增or编辑
      recordVersion: '', //版本号
      editAnswerGroupId: '', //编辑时答案ID
      tableData: [] //表格数据
    };
  },
  methods: {
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          align: 'center',
          width: 70,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 180,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.editAnswerGroup(props.row)}>
                  编辑
                </el-button>
                <el-divider direction="vertical" />
                <el-button size="mini" type="text" onClick={() => this.getAnswerByGroupId(props.row)}>
                  选项信息
                </el-button>
                <el-divider direction="vertical" />
                <el-button size="mini" type="text" disabled={props.row.dataSources == '10451002' || props.row.questionNumber > 0} onClick={() => this.delAnswerGroupById(props.row)}>
                  删除
                </el-button>
              </div>
            );
          }
        },
        {
          title: '答案组ID',
          dataIndex: 'answerGroupId',
          filterType: 'input'
        },
        {
          title: '答案名称',
          dataIndex: 'answerGroupName',
          filterType: 'input'
        },
        {
          title: '答案说明',
          dataIndex: 'answerGroupDescript',
          filterType: 'input'
        },
        {
          title: '是否启用',
          dataIndex: 'isValid',
          sorter: true,
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.isValid, '1004')}</div>;
          }
        },
        {
          title: '数据来源',
          dataIndex: 'dataSources',
          sorter: true,
          filterType: 'input',
          render: props => {
            return <div>{this.createDictText(props.row.dataSources, '1045')}</div>;
          }
        },
        {
          title: '使用次数',
          dataIndex: 'questionNumber',
          sorter: true,
          filterType: 'input'
        }
      ];
    },
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '答案名称',
          fieldName: 'answerName',
          placeholder: '答案名称'
        },
        {
          type: 'SELECT',
          label: '是否启用',
          fieldName: 'isValid',
          placeholder: '是否启用',
          itemList: this.createDictList('1004')
        },
        {
          type: 'SELECT',
          label: '数据来源',
          fieldName: 'dataSources',
          placeholder: '数据来源',
          itemList: this.createDictList('1045')
        }
      ];
    },
    createAnswerTableColums() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '选项内容',
          dataIndex: 'answer',
          filterType: 'input'
        },
        {
          title: '排列顺序',
          dataIndex: 'answerSort',
          filterType: 'input'
        },
        {
          title: '数据来源',
          dataIndex: 'dataSources',
          filterType: 'input',
          render: props => {
            return <span>{this.createDictText(props.row.dataSources, '1045')}</span>;
          }
        }
      ];
    },
    //搜索
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val, { xhrAbort: false });
      this.clearAnswerTable();
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    //保存answerTable的数据，用于清空
    tableChangeFun(val) {
      this.tableData = val;
    },
    //清空answerTable
    clearAnswerTable() {
      this.$nextTick(() => {
        this.AnswerTable.EXECUTE_DELETE(this.tableData);
      });
    },
    closeHandler() {
      this.visible = false;
    },
    //新增答案
    add() {
      this.visible = true;
      this.type = 'add';
      this.editAnswerGroupId = '';
    },
    //编辑答案
    editAnswerGroup(row) {
      this.visible = true;
      this.type = 'edit';
      this.editAnswerGroupId = row.answerGroupId + '';
      this.recordVersion = row.recordVersion + '';
    },
    //根据答案组ID获取选项
    getAnswerByGroupId(row) {
      let searchDetail = {
        answerGroupId: row.answerGroupId
      };
      this.fetchAnswerParams = Object.assign(searchDetail, { xhrAbort: false });
    },
    //删除答案
    delAnswerGroupById(row) {
      this.$confirm('是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          let searchDetail = {
            answerGroupId: row.answerGroupId
          };
          delAnswerGroupAndAnswerById(searchDetail).then(res => {
            if (res.resultCode == 200) {
              this.$message.success('删除成功');
              this.changeHandle({
                dataSources: 10451001 //来源厂端
              });
            }else{
              this.$message.error('删除失败');
            }
          });
        })
        .catch(() => {});
    }
  },
  created() {},
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
    this.AnswerTable = this.$refs.answerTable.$refs.pageTable;
  }
};
</script>

<style lang="less" scoped>
.dialog-form {
  width: 100%;
  margin: 0;
}

.el-checkbox-group .el-checkbox + .el-checkbox {
  margin: 0;
}

.box {
  border: 1px solid @borderColor;
  border-radius: @borderRadius;
  box-shadow: @boxShadow;
  padding: @modulePadding;
  margin-top: 20px;
}
</style>
